App.controller('pickMaterialController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {

    $scope.grid = {
        options : {
          bAuthWidth : false
        },
        add : function() {
            $scope.pickMaterial = {};
            Page.ajaxPost('stockType/stockTree.do', null, function (resp) {
                $scope.stockList = resp.data;
                Page.ajaxPost('department/tree.do', null, function (resp) {
                    $scope.depts = resp.data;
                    $scope.editModal.show();
                });
            });
        },
        update: function(row) {
            $scope.pickMaterial = row;
            $scope.pickMaterial.id = row.stockId;
            $scope.pickMaterial.deptId = row.pickDeptId;
            Page.ajaxPost('stockType/stockTree.do', null, function (resp) {
                $scope.stockList = resp.data;
                Page.ajaxPost('department/tree.do', null, function (resp) {
                    $scope.depts = resp.data;
                    $scope.editModal.show();
                });
            });
        },
        save : function() {
            Page.ajaxPost($scope.pickMaterial.billId ? 'pickMaterial/update.do' : 'pickMaterial/add.do', $scope.pickMaterial, function() {
                $scope.grid.query();
                $scope.grid.cancel();
            });
        },
        column_defs: [{
            target : 12,
            buttons : [
                {
                    name: '编辑',
                    class: 'btn-warning btm-sm',
                    icon: 'fa-edit',
                    event: 'update',
                    visiable : function(row) {
                        if(row.status == "1") {
                            return true;
                        }
                        return false;
                    }
                },
                {
                    name: '删除',
                    class: 'btn-danger',
                    icon: 'fa-close',
                    event: function(row) {
                        Page.confirm("确定要删除吗？", function() {
                            Page.ajaxPost('pickMaterial/delete.do', row, function() {
                                $scope.grid.query();
                            });
                        });
                    },
                    visiable : function(row) {
                        if(row.status == "1") {
                            return true;
                        }
                        return false;
                    }
                }
            ]
        }]
    };

    $scope.selectable = function(data) {
        if(data.id.indexOf("_") != -1) { //选择的部门
            return false;
        }
        $scope.pickMaterial.unit = data.unit;
        $scope.pickMaterial.stockId = data.id;
        $scope.pickMaterial.spec = data.spec;
        $scope.pickMaterial.stockName = data.name;
    };

    $scope.editModalObj = {
        ok : function() {
            $scope.validateForm.doValidate();
            if($scope.validateForm.$valid) {
                var pattern = /^\d+(\.\d*)?$/;
                if(!pattern.test($scope.pickMaterial.price)) {
                    Page.notice("请输入正确的单价!");
                    return;
                }
                Page.ajaxPost($scope.pickMaterial.billId ? 'pickMaterial/update.do' : 'pickMaterial/add.do', $scope.pickMaterial, function() {
                    Page.notice("操作成功!");
                    $scope.grid.query();
                    $scope.editModal.hide();
                });
            }
        },
        cancel : function() {
            $scope.editModal.hide();
        }
    }


}]);